package com.example.mymusic;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.FileObserver;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Random;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

/**
 * @author xky
 */
public class MusicService extends Service {
    public static MediaPlayer mediaPlayer = null;
    public static ArrayList<Music> listMusic;
    private Music music;

    private boolean isCallPlay;

    /**
     * 默认0顺序播放,1单曲循环,2随机播放
     */
    public static int state = 0;
    public static final int STATE_TYPE = 3;
    public static final int RANDOM_PLAY = 2, ORDER_PLAY = 0, SINGLE_CYCLE = 1;

    private final int NOTIFICATION_ID = 1;
    private final int SERVICE_MUSIC_ACTIVITY_TITLE = 11;
    private final int SERVICE_MUSIC_ACTIVITY_IMAGE = 21;


    /**
     * m_position  service里的position值
     */
    public static final int NOT_MUSIC = -1;
    public static int m_position = NOT_MUSIC;

    public static Bitmap bitmap = null;
    public static int m_duration;
    private int position;
    private final String TAG = "MusicServiceLog";
    private static final Uri ALBUM_ART_URI = Uri.parse("content://media/external/audio/albumart");

    /**
     * 当前音乐是否在播放
     */
    public static boolean musicState = false;

    private Notification notification;
    private RemoteViews remoteView;

    /**
     * NotificationManager是状态栏通知的管理类，负责发通知、清楚通知等
     */
    private NotificationManager manager;
    private MyReceiver myReceiver;

    MySqLiteOpenHelper helper;
    private FileObserver fileObserverOne;
    private FileObserver fileObserverTwo;


    public MusicService() {

        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            if (null == fileObserverOne) {
                String onePath = "/storage/emulated/0/音乐";
                fileObserverOne = new SdCardListener(onePath);
                fileObserverOne.startWatching();
            }
            if (null == fileObserverTwo) {
                String twoPath = "/storage/emulated/0/Music";
                fileObserverTwo = new SdCardListener(twoPath);
                fileObserverTwo.startWatching();
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private final IBinder myBinder = new MyBinder();

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public class MyBinder extends Binder {
        public MusicService getService() {
            return MusicService.this;
        }

        public boolean getMusicState() {
            return musicState;
        }

        public MediaPlayer getMediaPlayer() {
            return mediaPlayer;
        }

        public int getMDuration() {
            return m_duration;
        }

        public int getState() {
            return state;
        }

        public ArrayList<Music> getListMusic() {
            return listMusic;
        }

        public int getMPosition() {
            return m_position;
        }

        public void preMusic() {
            pre();
        }

        public void playPause() {
            play();
        }

        public void nextMusic() {
            next();
        }

        public void updateState() {
            setState();
        }

        public Bitmap getBitmap() {
            return bitmap;
        }

    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        //从intent中获取数据
        Bundle bundle = intent.getExtras();
        //反序列化，取出对象链表，从MainActivity来的
        listMusic = Utils.getMusic(this);
        assert bundle != null;
        position = bundle.getInt("position", 0);
        setMusicImage();

        int action = bundle.getInt("action", 0);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        assert telephonyManager != null;
        telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);

        helper = new MySqLiteOpenHelper(MusicService.this, "media_player_data.db", null, 1);
        SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();

        //点击歌名切歌
        if (m_position != NOT_MUSIC && action == MusicWidget.WIDGET_SERVICE_PLAY_PAUSE) {
            play();
        } else {
            Log.i(TAG, "onStartCommand: " + position + "--------" + m_position);
            //如果不是当前正在播放的歌曲，更改歌曲播放
            if (m_position != position) {
                m_position = position;

                sqliteDatabase.execSQL("update media_player_table set position_data=" + m_position + " where id=1");
                Log.e("点击歌名切歌的值:", String.valueOf(m_position));

                setMusicImage();
                music = listMusic.get(position);
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(music.path);
                    mediaPlayer.prepare();
                    mediaPlayer.start();

                    //如果点击了同一首歌就继续播放，不从头
                    if (m_duration != 0) {
                        mediaPlayer.seekTo(m_duration);
                        m_duration = 0;

                    }
                    musicState = true;
                    sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //监听播放完成后，根据选择的模式继续播放
            mediaPlayer.setOnCompletionListener(mp -> {
                switch (state) {
                    case SINGLE_CYCLE:
                        mediaPlayer.start();
                        break;
                    case RANDOM_PLAY:
                        Random random = new Random();
                        position = random.nextInt(listMusic.size());
                        playByPosition(position);
                        break;
                    default:
                        //没有选择或者是顺序播放，都是自动播放下一首
                        next();
                        break;
                }

                musicState = true;
                sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");
            });
            updateUI();
            updateWidget();
            createNotification();
        }
        return super.onStartCommand(intent, flags, startId);
    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void next() {
        listMusic = Utils.getMusic(this);

        if (SdCardListener.isDelete == 1) {
            delete();
        } else {

            if (position < 0 || (position + 1) >= (listMusic.size())) {
                position = 0;
            } else {
                position = position + 1;
            }

            m_position = position;

            SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
            sqliteDatabase.execSQL("update media_player_table set position_data=" + m_position + " where id=1");

            Log.i(TAG, "next: " + m_position);
            setMusicImage();
            music = listMusic.get(position);
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(music.path);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            musicState = true;
            sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");
        }

        updateUI();
        updateNotification();
        updateWidget();
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void pre() {

        listMusic = Utils.getMusic(this);

        if (SdCardListener.isDelete == 1) {
            delete();
        } else {

            if ((position - 1) < 0 || (position - 1) > (listMusic.size() - 1)) {
                position = listMusic.size() - 1;
            } else {
                position = position - 1;
            }

            m_position = position;

            SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
            sqliteDatabase.execSQL("update media_player_table set position_data=" + m_position + " where id=1");
            Log.e("点击上一首的值:", String.valueOf(m_position));

            Log.i(TAG, "pre: " + m_position);
            setMusicImage();
            music = listMusic.get(position);
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(music.path);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            musicState = true;
            sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");
        }

        updateUI();
        updateNotification();
        updateWidget();
    }

    /**
     * 随机播放函数
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void playByPosition(int position) {

        listMusic = Utils.getMusic(this);

        if (position < 0 || position >= listMusic.size()) {
            Toast.makeText(getApplicationContext(), "无该歌曲", Toast.LENGTH_SHORT).show();
            return;
        }
        m_position = position;

        SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
        sqliteDatabase.execSQL("update media_player_table set position_data=" + m_position + " where id=1");
        Log.e("随机播放状态的值:", String.valueOf(m_position));

        setMusicImage();
        music = listMusic.get(position);
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(music.path);
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (IOException e) {
            e.printStackTrace();
        }
        musicState = true;
        sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");


        updateUI();
        updateNotification();
        updateWidget();
    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void playBy0() {

        listMusic = Utils.getMusic(this);

        m_position = 0;
        position = 0;
        SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
        sqliteDatabase.execSQL("update media_player_table set position_data=" + m_position + " where id=1");


        if (listMusic.size() == 0) {
            Toast.makeText(getApplicationContext(), "没有检测到可以播放的歌曲。", Toast.LENGTH_SHORT).show();
        } else {

            setMusicImage();
            music = listMusic.get(position);
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(music.path);
                mediaPlayer.prepare();
                mediaPlayer.start();

            } catch (IOException e) {
                e.printStackTrace();
            }
            musicState = true;
            sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");
        }


        updateUI();
        updateNotification();
        updateWidget();
    }

    /**
     * 暂停还是播放
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void play() {
        SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
        listMusic = Utils.getMusic(this);

        if (SdCardListener.isDelete == 1) {
            delete();
        } else {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                musicState = false;
                sqliteDatabase.execSQL("update media_player_table set player_state_data=0 where id=1");

            } else {
                mediaPlayer.start();
                musicState = true;
                sqliteDatabase.execSQL("update media_player_table set player_state_data=1 where id=1");

            }
        }


        updateUI();
        updateNotification();
        updateWidget();


    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void delete() {
        playBy0();
        SdCardListener.isDelete = 0;
        Toast.makeText(getApplicationContext(), "检测歌曲信息被更改,回到第一首歌曲。", Toast.LENGTH_SHORT).show();
        Log.e("我是service：", String.valueOf(SdCardListener.isDelete));
    }


    private void updateUI() {
        Log.i(TAG, "updateUI");
        //以广播方式发送update_ui意图
        Intent intent = new Intent();
        intent.setAction("update_ui");
        sendBroadcast(intent);
    }

    private void updateWidget() {
        //隐式启动能响应设置的这个action的活动，需在清单文件AndroidManifest.xml中设置action属性
        Intent intent = new Intent(this, MusicWidget.class);
        //以广播方式发送update意图
        intent.setAction("update");
        sendBroadcast(intent);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        m_position = NOT_MUSIC;

        //动态设置该广播接收者接收的Action
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("MusicService");
        myReceiver = new MyReceiver();

        registerReceiver(myReceiver, intentFilter);
//        Log.i(TAG, "onCreate");


    }

    /**
     * 接收到广播后的行为
     */
    private class MyReceiver extends BroadcastReceiver {

        @RequiresApi(api = Build.VERSION_CODES.Q)
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "MyReceiver::onReceive: ");
            //取出Intent中附加的数据，接受来自通知栏的广播，按钮的事件
            String button = intent.getStringExtra("button");
            switch (button) {
                case "play":
                    play();
                    break;
                case "pre":
                    pre();
                    break;
                case "next":
                    next();
                    break;
                case "state":
                    state = (state + 1) % STATE_TYPE;
                    break;
                default:
                    break;
            }
            updateUI();
            updateWidget();
            updateNotification();
        }
    }


    /**
     * 解绑服务
     */
    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void updateNotification() {
        //自定义Notification的时候需要用到RemoteViews，传入布局文件
        remoteView.setTextViewText(R.id.notification_title, music.name);
        //设置控件内容,点击监听
        remoteView.setImageViewBitmap(R.id.music_image, bitmap);
        remoteView.setOnClickPendingIntent(R.id.notification_title, getPendingIntent(this, SERVICE_MUSIC_ACTIVITY_TITLE));
        remoteView.setOnClickPendingIntent(R.id.music_image, getPendingIntent(this, SERVICE_MUSIC_ACTIVITY_IMAGE));
        if (musicState) {
            remoteView.setImageViewResource(R.id.play_pause, R.mipmap.pause);
        } else {
            remoteView.setImageViewResource(R.id.play_pause, R.mipmap.play);
        }
        notification.contentView = remoteView;
        //作为普通提醒启动
        //NotificationManager 是一个系统Service，必须通过 getSystemService()方法来获取
        NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
        //更新Notification
        assert manager != null;
        manager.notify(NOTIFICATION_ID, notification);
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void createNotification() {
        //获取NotificationManager
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //使用构造器创建通知栏
        NotificationCompat.Builder builder;

        //Android8.0也就是API26开始要求通知设置Channel,设置一个id就可以了，否则会报错
        int channelId = 1;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(String.valueOf(channelId), "channel_name", NotificationManager.IMPORTANCE_MIN);
            manager.createNotificationChannel(channel);
        }
        builder = new NotificationCompat.Builder(this, String.valueOf(channelId));

        //创建RemoteViews，包名和布局资源
        remoteView = new RemoteViews(getPackageName(), R.layout.notification);
        //设置控件内容
        remoteView.setImageViewBitmap(R.id.music_image, bitmap);
        remoteView.setTextViewText(R.id.notification_title, music.name);
        remoteView.setOnClickPendingIntent(R.id.music_image, getPendingIntent(this, SERVICE_MUSIC_ACTIVITY_IMAGE));
        remoteView.setOnClickPendingIntent(R.id.notification_title, getPendingIntent(this, SERVICE_MUSIC_ACTIVITY_TITLE));
        int playPause = 0;
        remoteView.setOnClickPendingIntent(R.id.play_pause, getPendingIntent(this, playPause, "play"));
        int preMusic = 1;
        remoteView.setOnClickPendingIntent(R.id.pre_music, getPendingIntent(this, preMusic, "pre"));
        int nextMusic = 2;
        remoteView.setOnClickPendingIntent(R.id.next_music, getPendingIntent(this, nextMusic, "next"));
        if (musicState) {
            remoteView.setImageViewResource(R.id.play_pause, R.mipmap.pause);
        } else {
            remoteView.setImageViewResource(R.id.play_pause, R.mipmap.play);
        }

        //添加参数
        builder.setContent(remoteView)
                .setOngoing(false)
                .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis());

        //返回的notification对象
        notification = builder.build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;

        //开启前台服务
        startForeground(NOTIFICATION_ID, notification);
    }

    /**
     * 延迟执行的intent，意图，用于设置Notification的点击事件
     * requestCode是用来标识请求的来源
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    private PendingIntent getPendingIntent(Context context, int requestCode) {


        //将数据传去MusicActivity，点击跳转到详情界面的意图
        Bundle bundle = new Bundle();
        listMusic = Utils.getMusic(this);
        bundle.putParcelableArrayList("listMusic", listMusic);
        bundle.putInt("position", m_position);
        Intent intent = new Intent();

        //把要传递的数据暂存在Intent中，启动了另一个活动后，只需要把这些数据再从Intent中取出就可以了
        intent.putExtras(bundle);
        //不需要返回值的跳转，显式intent
        intent.setClass(this, MusicActivity.class);
        return PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    private PendingIntent getPendingIntent(Context context, int requestCode, String button) {
        //延迟意图用于发送一个广播
        Intent intent = new Intent();
        intent.setAction("MusicService");
        intent.putExtra("button", button);
        return PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    @Override
    public void onDestroy() {

        Log.i("onDestroy:", "onDestroy");
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
            musicState = false;
            helper = new MySqLiteOpenHelper(MusicService.this, "media_player_data.db", null, 1);
            SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
            sqliteDatabase.execSQL("update media_player_table set player_state_data=0 where id=1");
        }
        if (remoteView != null) {
            manager.cancel(NOTIFICATION_ID);
        }

        unregisterReceiver(myReceiver);
        mediaPlayer = null;

        fileObserverOne.stopWatching();
        fileObserverTwo.stopWatching();
        super.onDestroy();
    }


    /**
     * 返回一个myBinder对象
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }


    public void setState() {
        state = (state + 1) % STATE_TYPE;
        updateUI();
    }


    public static Bitmap getArtWorkFormFile(ContentResolver resolver, long songid, int album) {
        Bitmap bitmap = null;
        if (album < 0 && songid < 0) {
            throw new IllegalArgumentException("Must specify an album or song");
        }
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            FileDescriptor fileDescriptor = null;
            if (album < 0) {
                Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
                ParcelFileDescriptor parcelFileDescriptor = resolver.openFileDescriptor(uri, "r");
                if (parcelFileDescriptor != null) {
                    fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                }
            } else {
                Uri uri = ContentUris.withAppendedId(ALBUM_ART_URI, album);
                ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
                if (pfd != null) {
                    fileDescriptor = pfd.getFileDescriptor();
                }
            }

            options.inSampleSize = 1;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
            options.inSampleSize = calculateInSampleSize(options, 50, 50);
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bitmap;
    }


    public static Bitmap getArtwork(ContentResolver resolver, long songId, int albumId, boolean small) {
        if (albumId < 0) {
            if (songId < 0) {
                Bitmap bitmap = getArtWorkFormFile(resolver, songId, albumId);
                if (bitmap != null) {
                    return bitmap;
                }
            }
        }

        Uri uri = ContentUris.withAppendedId(ALBUM_ART_URI, albumId);
        if (uri != null) {
            try {
                InputStream inputStream = resolver.openInputStream(uri);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(inputStream, null, options);
                if (small) {
                    options.inSampleSize = calculateInSampleSize(options, 50, 50);
                } else {
                    options.inSampleSize = calculateInSampleSize(options, 600, 600);
                }
                options.inJustDecodeBounds = false;

                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                inputStream = resolver.openInputStream(uri);
                return BitmapFactory.decodeStream(inputStream, null, options);
            } catch (FileNotFoundException e) {
                Bitmap bitmap = getArtWorkFormFile(resolver, songId, albumId);
                if (bitmap != null) {
                    if (bitmap.getConfig() == null) {
                        bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
                    }
                } else {
                    return null;
                }
                return bitmap;
            }
        }
        return null;
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    /**
     * 获取歌曲图片
     */

    private void setMusicImage() {

        bitmap = getArtwork(getContentResolver(), listMusic.get(position).id, listMusic.get(position).albumId, false);
        if (bitmap == null) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(getResources(), R.drawable.yuantu2, options);
            options.inSampleSize = calculateInSampleSize(options, 500, 500);
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yuantu2, options);
        }
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
            switch (state) {
                //来电，应当停止音乐
                case TelephonyManager.CALL_STATE_RINGING:
                    if (musicState) {
                        mediaPlayer.pause();
                        musicState = false;
                        isCallPlay = true;

                        SQLiteDatabase sqliteDatabase = helper.getWritableDatabase();
                        sqliteDatabase.execSQL("update media_player_table set player_state_data=0 where id=1");
                    }
                    break;
                //无电话状态
                case TelephonyManager.CALL_STATE_IDLE:
                    if (isCallPlay) {
                        isCallPlay = false;
                        mediaPlayer.start();
                    }
                    break;
                default:
                    break;
            }

            super.onCallStateChanged(state, phoneNumber);
        }
    }

}
